//
//  ZJCourseSelectionsView.m
//  ZhongWangYuanApp
//
//  Created by ZJ on 2018/6/3.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ZJCourseSelectionsView.h"

@interface ZJCourseSelectionsView ()
{
    NSArray *_dataArray;//传进来的数据
    NSString *_courseID;//转过来的课程ID
}
/** 选集 */
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation ZJCourseSelectionsView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self initUI];
    }
    return self;
}

- (void)initUI{
    self.backgroundColor = [UIColor themeWhiteColor];
    [self addSubview:self.titleLabel];
    
    self.titleLabel.sd_layout
    .topSpaceToView(self, 12 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self, 12 * [UIScreen zj_screenWScale])
    .heightIs(26 * [UIScreen zj_screenHScale])
    .widthIs([NSString zj_sizeOfString:self.titleLabel.text font:self.titleLabel.font width:200].width);
    
    [self setupAutoHeightWithBottomView:self.titleLabel bottomMargin:12 * [UIScreen zj_screenHScale]];
}

- (void)setCourseID:(NSString *)courseID{
    _courseID = courseID;
}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    for (id View in self.subviews) {
        if ([View isKindOfClass:[UIButton class]]) {
            [View removeFromSuperview];
        }
    }
    
    CGFloat space = 10 * [UIScreen zj_screenWScale];
    
    for (int i = 0; i < _dataArray.count; i ++) {
        ZJSelectionsMdel *model = dataArray[i];
        UIButton *button = [UIButton new];
        [button setTitle:model.shortname forState:UIControlStateNormal];
        button.titleLabel.font = self.titleLabel.font;
        button.tag = i;
        if ([_courseID isEqualToString:model.coursewareid]) {
            [self setButton:button IsSelect:YES];
        }else{
            [self setButton:button IsSelect:NO];
        }
        [button addTarget:self action:@selector(action_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button.sd_layout
        .topEqualToView(self.titleLabel)
        .widthIs(61 * [UIScreen zj_screenWScale])
        .heightRatioToView(self.titleLabel, 1)
        .leftSpaceToView(self, (12 + 26) * [UIScreen zj_screenWScale] + space + i * (space + 61 * [UIScreen zj_screenWScale]));
    }
}

- (void)setButton:(UIButton *)button IsSelect:(BOOL)isSelect{
    button.layer.cornerRadius = 3.0f;
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor zj_colorWithHexString:@"c9c9c9"].CGColor;
    [button setTitleColor:[UIColor textGrayColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor themeWhiteColor] forState:UIControlStateSelected];
    button.selected = isSelect;
    if (isSelect) {
        button.layer.borderWidth = 0;
        button.backgroundColor = [UIColor themeColor];
    }else{
        button.layer.borderWidth = 1;
        button.backgroundColor = [UIColor themeWhiteColor];
    }
}

#pragma mark - 点击事件
- (void)action_buttonClick:(UIButton *)sender{
    if (sender.selected) {
        return;
    }
    ZJSelectionsMdel *model = _dataArray[sender.tag];
    if (self.delelgate) {
        [self.delelgate ZJCourseSelectionsViewDelegate_buttonClick_CourseID:model.coursewareid];
    }
}

#pragma mark - Setter


#pragma mark - Getters
- (UILabel *)titleLabel{
    if (_titleLabel == nil){
        _titleLabel = [UILabel new];
        _titleLabel.text = @"选集";
        _titleLabel.textColor = [UIColor textGrayColor];
        _titleLabel.font = LYPBOLDFONT(15);
    }
    return _titleLabel;
}


@end
